import { defineStore } from 'pinia'

export const useUserStore = defineStore('user', {
    state: () => ({
        token: null,
        id: null,
        username: null
    }),
    actions: {
        setUser(token, id, username) {
            this.token = token
            this.id = id
            this.username = username

            // 保存到sessionStorage
            sessionStorage.setItem('token', token)
            sessionStorage.setItem('id', id)
            sessionStorage.setItem('username', username)
        },
        loadFromStorage() {
            this.token = sessionStorage.getItem('token')
            this.id = sessionStorage.getItem('id')
            this.username = sessionStorage.getItem('username')
        },
        logout() {
            this.token = null
            this.id = null
            this.username = null

            sessionStorage.removeItem('token')
            sessionStorage.removeItem('id')
            sessionStorage.removeItem('username')
        }
    }
})